package com.zhitan.ems.common.extra.utils;

import java.util.Map;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class HttpUtils {
	public static JSONObject httpGet(String url, Map<String, Object> querys) {
		String str=HttpUtil.get(url, querys);
		if(JSONUtil.isTypeJSON(str)) {
			log.info("------httpGet-------"+str);
			return JSONUtil.parseObj(str);
		}
		log.error("------httpGet-------"+str);
		return null;
	}
	public static JSONObject httpGet(String url) {
		String str=httpGetStr(url);
		if(JSONUtil.isTypeJSON(str)) {
			log.info("------httpGet-------"+str);
			return JSONUtil.parseObj(str);
		}
		log.error("------httpGet-------"+str);
		return null;
	}
	public static String httpGetStr(String url) {
		return HttpUtil.get(url);
	}
	public static JSONObject doPostJson(String url,JSONObject jsonParam) {
		HttpRequest httpRequest=HttpUtil.createPost(url);
		httpRequest.body(JSONUtil.toJsonStr(jsonParam));
		String str=httpRequest.execute().body();
		if(JSONUtil.isTypeJSON(str)) {
			log.info("------httpGet-------"+str);
			return JSONUtil.parseObj(str);
		}
		log.error("------doPostJson-------"+str);
		return null;
	}
	public static JSONObject doPostJson(String url, Map<String, String> headMap, JSONObject jsonParam) {
		HttpRequest httpRequest=HttpUtil.createPost(url).addHeaders(headMap);
		httpRequest.body(JSONUtil.toJsonStr(jsonParam));
		String str=httpRequest.execute().body();
		if(JSONUtil.isTypeJSON(str)) {
			log.info("------httpGet-------"+str);
			return JSONUtil.parseObj(str);
		}
		log.error("------doPostJson-------"+str);
		return null;
	}
}
